home *** CD-ROM | disk | FTP | other *** search
/ Nebula 2 / Nebula Two.iso / SourceCode / MiscKit1.7.1 / MiscKit / Palettes / MiscTeePalette / MiscDistributor.subproj / MiscDistributor.m < prev    next >
Encoding:
Text File  |  1994-10-24  |  3.2 KB  |  126 lines

  1. //
  2. //    MiscDistributor.m -- An object which will pass a message on to any of
  3. //            the objects connected to it, even across .nib file boundaries.
  4. //
  5. //        Written by Don Yacktman.  Copyright 1994 by Don Yacktman.
  6. //                Version 1.0  All rights reserved.
  7. //
  8. //        This notice may not be removed from this source code.
  9. //
  10. //    This object is included in the MiscKit by permission from the author
  11. //    and its use is governed by the MiscKit license, found in the file
  12. //    "LICENSE.rtf" in the MiscKit distribution.  Please refer to that file
  13. //    for a list of all applicable permissions and restrictions.
  14. //    
  15.  
  16. #import <appkit/appkit.h>
  17. #import "MiscDistributor.h"
  18. #define INSUBPROJ
  19. #import "../_MiscDistributorConnection.h"
  20. #import "../MiscDistributorPrivate.h"
  21.  
  22. static id distributors = nil; // to be a HashTable
  23.  
  24. #define CURRENT_VERSION 1
  25. #define STARTINGCAPACITY 512    // you may need this a lot larger.
  26.  
  27. @implementation MiscDistributor(private)
  28.  
  29. - _connectionList { return connections; }
  30. - _makeConnections
  31. {
  32.     int i;
  33.     for (i=0; i<[connections count]; i++) {
  34.         id aConnection = [connections objectAt:i];
  35.         [self connectToDistributorNamed:[aConnection connectionName]
  36.                 direction:[aConnection direction]];
  37.     }
  38.     return self;
  39. }
  40.  
  41. @end
  42.  
  43. @implementation MiscDistributor
  44.  
  45. + initialize
  46. {
  47.     // set up class version number and set up the waiting list and
  48.     // distributors dictionary
  49.     if (self == [MiscDistributor class]) {
  50.         [MiscDistributor setVersion:CURRENT_VERSION];
  51.         if (!distributors) distributors = [[HashTable alloc]
  52.                 initKeyDesc:"%" valueDesc:"@" capacity:STARTINGCAPACITY];
  53.     }
  54.     return self;
  55. }
  56.  
  57. + findDistributorNamed:(const char *)aString
  58. {
  59.     // extract object from the hash table, creating if needed.
  60.     id theDistributor; NXAtom key;
  61.     if (!aString) return nil;
  62.     if (!strlen(aString)) return nil;
  63.     if (!distributors) distributors = [[HashTable alloc]
  64.             initKeyDesc:"%" valueDesc:"@" capacity:STARTINGCAPACITY];
  65.     key = NXUniqueString(aString);
  66.     // OK, have a name.  Is it in the table?
  67.     theDistributor = [distributors valueForKey:key];
  68.     if (!theDistributor) {
  69.         theDistributor = [[MiscTee alloc] init];
  70.         [distributors insertKey:key value:theDistributor];
  71.     }
  72.     return theDistributor;
  73. }
  74.  
  75. - init
  76. {
  77.     [super init];
  78.     connections = [[List alloc] init];
  79.     return self;
  80. }
  81.  
  82. - connectToDistributorNamed:(const char *)aString
  83.         direction:(MiscDCDirection)direction
  84. {    // sets up an actual connection
  85.     id theTee = [[self class] findDistributorNamed:aString];
  86.     BOOL doIn = NO; BOOL doOut = NO;
  87.  
  88.     switch (direction) {
  89.         case MiscIn        : { doIn = YES;                    break; }
  90.         case MiscOut    : {                doOut = YES;    break; }
  91.         case MiscInout    : { doIn = YES;    doOut = YES;    break; }
  92.         default            : {                                break; }
  93.     }
  94.     if (doIn) [theTee addConnection:self with:@selector(ping:)];
  95.     if (doOut) [self addConnection:theTee with:@selector(ping:)];
  96.     return self;
  97. }
  98.  
  99. - awakeFromNib
  100. {
  101.     [self _makeConnections];
  102.     return self;
  103. }
  104.  
  105. - read:(NXTypedStream *)stream
  106. {
  107.     int version;
  108.  
  109.     [super read:stream];
  110.     version = NXTypedStreamClassVersion(stream,"MiscDistributor");
  111.     if (version == CURRENT_VERSION) {
  112.         connections = NXReadObject(stream);
  113.     }
  114.     if (!connections) connections = [[List alloc] init];
  115.     return self;
  116. }
  117.  
  118. - write:(NXTypedStream *)stream
  119. {
  120.     [super write:stream];
  121.     NXWriteObject(stream, connections);
  122.     return self;
  123. }
  124.  
  125. @end
  126.